package com.fram.controller.admin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseAdminController;
import com.fram.entity.NewsList;
import com.fram.service.admin.NewsListService;

@RestController
@RequestMapping("/admin/{version}/auth/newslist")
public class NewsListController extends BaseAdminController {

	private static Logger LOG = LoggerFactory.getLogger(NewsListController.class);

	@Autowired
	private NewsListService newsListService;

	/** 新增 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	private ResponseMsg add(@RequestBody NewsList request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			newsListService.add(request);
		} catch (ProException e) {
			LOG.error("-后台-增加新闻->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台-增加新闻--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 查询列表 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	private ResponseMsg list(@RequestBody PageBean<NewsList> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getParam() == null) {
				request.setParam(new NewsList());
			}
			if (getAdminParamUserId() != null) {
				request.getParam().setDistributorId(getDistributorId());
			}
			msg.setData(newsListService.list(request));
		} catch (ProException e) {
			LOG.error("-后台-新闻展示->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台-新闻展示--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	private ResponseMsg update(@RequestBody NewsList request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			newsListService.update(request);
		} catch (ProException e) {
			LOG.error("-后台-新闻修改->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台-新闻修改--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 详情 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/details", method = RequestMethod.GET)
	private ResponseMsg details(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();

		try {
			msg.setData(newsListService.details(id));
		} catch (ProException e) {
			LOG.error("-后台-新闻详情->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台-新闻详情--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

}
